/**
 * 入库记录storeInRecord.js
 * 版本号： v1.0.0
 */
$(function () {

    /**
     * 时间选中事件
     */
    $("#startDate").datebox({
        onSelect : function(beginDate){
            $('#endDate').datebox().datebox('calendar').calendar({
                validator: function(date){
                    return beginDate<=date;
                }
            });
        }
    });

    var lastChecked = null;
    $("#storeInRecord_dataList").datagrid({
        fitColumns: true,
        fit: true,
        method: "POST",
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        border: false,
        pageSize: 10,
        pageList: [10, 30, 50, 100],
        url: CONTEXT_PATH + '/storeInRecord/storeInRecordList.do',
        idField: 'sameRecord',
        frozenColumns: [[
            {field: 'sameRecord', checkbox: true},
        ]],
        columns: [
            [
                {field: 'prName', title: '项目名称', width: 180, fixed: true},
                {field: 'coName', title: '合同名称', width: 180, fixed: true},
                {field: 'coNumber', title: '合同编号', width: 120,fixed: true},
                {field: 'prLeader', title: '项目负责人', width: 80,fixed: true},
                {field: 'storeWay', title: '入库方式', width: 80,fixed:true,formatter:function (val) {
                        if(val == "1"){
                            return "流程入库";
                        }
                        if(val == "2"){
                            return "填写入库";
                        }
                    }},
                {field: 'pushPerson', title: '入库人', width: 70,fixed:true},
                {field: 'pushDate', title: '入库时间', width: 130,align:'left'}
            ]
        ],
        onCheck:function(rowIndex, rowData){
            var row = $("#storeInRecord_dataList").datagrid("getChecked");
            if(row && rowIndex == lastChecked){
                $('#storeInRecord_dataList').datagrid("unselectAll");//取消选中当前所有行
                lastChecked = null;
            }else{
                lastChecked = rowIndex;
            }
        },
        onLoadSuccess: function (data) {
            if (data.rows.length > 0) {
                lastChecked = null;
                $("#storeInRecord_dataList").datagrid("selectRow", 0);
            }
        }
    });

    //定义可操作按钮
    $("#storeInRecord_menu").dialog({
        title: '',
        width: 'auto',
        height:35,
        closed: false,
        cache: false,
        inline: true,
        modal: false,
        border: false,
        toolbar: createToolbar()
    });
});

/**
 * 创建toolbar按钮组
 * @returns {Array}
 */
function createToolbar() {
    var toolbar = [];
    var falg = false;
    for(var j = 0; j < btnOptions.length; j++ ){
        if(false != btnOptions[j].value){
            falg = true;
            break;
        }
    }
    if(!falg){
        //不存在按钮
        toolbar.push({});
    }else{
        //存在按钮
        for(var i = 0; i < btnOptions.length; i++){
            if(false != btnOptions[i].value){
                //查询按钮
                if("searchBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler: function () {
                            storeInRecord_findList();
                        }
                    });
                }
                //查看详细列表按钮
                if("viewBtn"  == btnOptions[i].key){
                    toolbar.push({
                        id:btnOptions[i].value.meId,
                        text:btnOptions[i].value.name,
                        iconCls:btnOptions[i].value.iconCls,
                        handler:function(){
                            storeInRecord_view($(this).linkbutton("options").iconCls);
                        }
                    });
                }
            }
        }
    }
    return toolbar;
}

//入库记录查询
function storeInRecord_findList() {
    refreshPageNumber("storeInRecord_dataList");
    $("#storeInRecord_dataList").datagrid("reload",{
        prName:$("#prName").val(),
        coName:$("#coName").val(),
        coNumber:$("#coNumber").val(),
        startDate:$("#startDate").datebox("getValue"),
        endDate:$("#endDate").datebox("getValue"),
        storeWay:$("#storeWay").combobox("getValue")
    });
    //清楚选中项
    $("#storeInRecord_dataList").datagrid("clearSelections");
}


/**
 * 查看入库记录列表
 */
function storeInRecord_view(iconCls){
    var row = $("#storeInRecord_dataList").datagrid("getSelected");
    if (row == null) {
        $.messager.show({ title: '操作提示', msg: "请选择需要查看的入库记录", timeout: 3000, showType: "slide" });
        return;
    }
    //IFRAM打开窗口
    layoutSub(rkjllb.id,rkjllb.url + row.sameRecord, iconCls, rkjllb.closable,rkjllb.title);
}